import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

public class EmployeeManagementGUI extends JFrame {
    private JTextField nameField;
    private JTextField idNumberField;
    private JTable employeeTable;
    private EmployeeInfoDAO employeeDAO;

    public EmployeeManagementGUI() {
        employeeDAO = new EmployeeInfoDAO();

        setTitle("员工管理系统");
        setSize(600, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setLayout(new BorderLayout());

        JPanel inputPanel = createInputPanel();
        add(inputPanel, BorderLayout.NORTH);

        employeeTable = new JTable();
        updateTableData();
        JScrollPane scrollPane = new JScrollPane(employeeTable);
        add(scrollPane, BorderLayout.CENTER);

        JPanel buttonPanel = createButtonPanel();
        add(buttonPanel, BorderLayout.SOUTH);
    }

    private JPanel createInputPanel() {
        JPanel panel = new JPanel();
        panel.setLayout(new GridLayout(2, 2));

        JLabel nameLabel = new JLabel("员工姓名:");
        panel.add(nameLabel);
        nameField = new JTextField();
        panel.add(nameField);

        JLabel idNumberLabel = new JLabel("员工身份证号:");
        panel.add(idNumberLabel);
        idNumberField = new JTextField();
        panel.add(idNumberField);

        return panel;
    }

    private JPanel createButtonPanel() {
        JPanel panel = new JPanel();

        JButton addButton = new JButton("添加员工");
        addButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addEmployee();
            }
        });
        panel.add(addButton);
        return panel;
    }

    private void addEmployee() {
        String name = nameField.getText();
        String idNumber = idNumberField.getText();
        EmployeeInfo employee = new EmployeeInfo();
        employee.setName(name);
        employee.setIdNumber(idNumber);
        employeeDAO.addEmployee(employee);
        clearFields();
        updateTableData();
    }

    private void clearFields() {
        nameField.setText("");
        idNumberField.setText("");
    }

    private void updateTableData() {
        List<EmployeeInfo> employees = employeeDAO.getAllEmployees();
        String[] columnNames = {"员工编号", "姓名", "身份证号"};
        Object[][] data = new Object[employees.size()][3];
        for (int i = 0; i < employees.size(); i++) {
            EmployeeInfo employee = employees.get(i);
            data[i][0] = employee.getEmployeeId();
            data[i][1] = employee.getName();
            data[i][2] = employee.getIdNumber();
        }
        employeeTable.setModel(new JTable(data, columnNames).getModel());
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new EmployeeManagementGUI().setVisible(true);
            }
        });
    }
}
